package com.eva.admin.api.cms;

import com.eva.admin.biz.cms.resource.CmsResourceGroupBiz;
import com.eva.admin.biz.cms.resource.dto.CreateCmsResourceGroupDTO;
import com.eva.admin.biz.cms.resource.dto.QueryCmsResourceGroupDTO;
import com.eva.admin.biz.cms.resource.dto.UpdateCmsResourceGroupDTO;
import com.eva.admin.biz.cms.resource.vo.CmsResourceGroupVO;
import com.eva.dao.cms.model.CmsResourceGroup;
import com.eva.framework.cache.clean.CleanCache;
import com.eva.framework.common.api.BaseController;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.service.cms.CmsResourceGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "资源管理")
@RestController
@RequestMapping("/cms/res/group")
public class CmsResourceGroupController extends BaseController {

    @Resource
    private CmsResourceGroupBiz cmsResourceGroupBiz;

    @Resource
    private CmsResourceGroupService cmsResourceGroupService;

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @ContainPermissions("cms:resource:group:create")
    public ApiResponse<?> create(@RequestBody CreateCmsResourceGroupDTO dto) {
        cmsResourceGroupBiz.create(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("删除")
    @CleanCache(keys = "resource:group:*:resources")
    @GetMapping("/delete/{id}")
    @ContainPermissions("cms:resource:group:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        cmsResourceGroupBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @CleanCache(keys = "resource:group:*:resources")
    @GetMapping("/delete/batch")
    @ContainPermissions("cms:resource:group:delete")
    public ApiResponse<?> deleteByIdInBatch(@RequestParam String ids) {
        cmsResourceGroupBiz.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("修改")
    @CleanCache(keys = "resource:group:*:resources")
    @PostMapping("/updateById")
    @ContainPermissions("cms:resource:group:update")
    public ApiResponse<?> updateById(@RequestBody UpdateCmsResourceGroupDTO dto) {
        cmsResourceGroupBiz.updateById(dto);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("cms:resource:group:query")
    public ApiResponse<PageData<CmsResourceGroupVO>> findPage (@RequestBody PageWrap<QueryCmsResourceGroupDTO> pageWrap) {
        return ApiResponse.success(cmsResourceGroupBiz.findPage(pageWrap));
    }

    @ApiOperation("查询所有")
    @GetMapping("/all")
    public ApiResponse<List<CmsResourceGroup>> findAll () {
        return ApiResponse.success(cmsResourceGroupService.findAll());
    }
}
